// DlgEditPriority.cpp : implementation file
//

#include "stdafx.h"
#include "Celsus.h"
#include "DlgEditPriority.h"
#include "DlgPickList.h"

// CDlgEditPriority dialog

IMPLEMENT_DYNAMIC(CDlgEditPriority, CDialog)

CDlgEditPriority::CDlgEditPriority(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgEditPriority::IDD, pParent)
	, m_szID(_T(""))
	, m_dServiceFee(0)
	, m_nInHours(0)
	, m_szDescription(_T(""))
	, m_bUserListedRequest(FALSE)
	, m_bUserListedResubmit(FALSE)
	, m_szIconFile(_T(""))
	, m_szTextColor(_T(""))
	, m_szTextColorOld(_T(""))
	, m_szBackGroundColor(_T(""))
	, m_szBackGroundColorOld(_T(""))
	, m_szPrintNotice(_T(""))
{
	m_bAddPriority = FALSE;
	m_nID = -1;
	m_nIndexID = -1;
}

CDlgEditPriority::~CDlgEditPriority()
{
}

void CDlgEditPriority::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT_ID, m_szID);
	DDX_Text(pDX, IDC_EDIT_SERVICE_FEE, m_dServiceFee);
	DDX_Text(pDX, IDC_EDIT_IN_HOURS, m_nInHours);
	DDX_Text(pDX, IDC_EDIT_DESCRIPTION, m_szDescription);
	DDX_Check(pDX, IDC_CHECK_USER_LISTED, m_bUserListedRequest);
	DDX_Check(pDX, IDC_CHECK_USER_LISTED_RESUBMIT, m_bUserListedResubmit);
	DDX_Text(pDX, IDC_EDIT_ICON_FILE, m_szIconFile);
	DDX_Text(pDX, IDC_EDIT_TEXT_COLOR, m_szTextColor);
	DDX_Text(pDX, IDC_EDIT_TEXT_COLOR_OLD, m_szTextColorOld);
	DDX_Text(pDX, IDC_EDIT_BACKGROUND_COLOR, m_szBackGroundColor);
	DDX_Text(pDX, IDC_EDIT_BACKGROUND_COLOR_OLD, m_szBackGroundColorOld);

	DDV_MaxChars(pDX, m_szIconFile, 255);
	DDV_MaxChars(pDX, m_szTextColor, 8);
	DDV_MaxChars(pDX, m_szTextColorOld, 8);
	DDV_MaxChars(pDX, m_szBackGroundColor, 8);
	DDV_MaxChars(pDX, m_szBackGroundColorOld, 8);
	DDX_Control(pDX, IDC_SPIN_RECORD, m_ctrlRecord);
	DDX_Text(pDX, IDC_EDIT_PRINT_NOTICE, m_szPrintNotice);
}


BEGIN_MESSAGE_MAP(CDlgEditPriority, CDialog)
	ON_BN_CLICKED(IDC_BUTTON_DELETE, &CDlgEditPriority::OnBnClickedButtonDelete)
	ON_BN_CLICKED(IDC_BUTTON_BROWSE, &CDlgEditPriority::OnBnClickedButtonBrowse)
	ON_BN_CLICKED(IDC_BUTTON_APPLY, &CDlgEditPriority::OnBnClickedButtonApply)
	ON_NOTIFY(UDN_DELTAPOS, IDC_SPIN_RECORD, &CDlgEditPriority::OnDeltaposSpinRecord)
END_MESSAGE_MAP()


// CDlgEditPriority message handlers

BOOL CDlgEditPriority::OnInitDialog()
{
	CDialog::OnInitDialog();

	CStringArray szaPriority;
	PRIORITY_LOOKUP Priority;

	appConfig.m_SQL.GetDescriptions( CMySQLEx::priority, &szaPriority, CMySQLEx::list, &m_naID );

	// set spin control
	m_ctrlRecord.SetRange( 0, m_naID.GetCount() - 1 );

	// check if adding
	if( m_bAddPriority )
	{
		return TRUE;
	}
	else
	{
		//GetDlgItem( IDC_EDIT_ID )->EnableWindow( FALSE );
	}

	// pick list
	if( m_nID < 0 )
	{
		// show pick list
		CDlgPickList PickListDlg;
		PickListDlg.m_pszaStrings = &szaPriority;
		if( PickListDlg.DoModal() == IDOK )
			m_nID = _tstol( PickListDlg.m_szString.Left( PickListDlg.m_szString.Find( _T(" ") )));
		else
		{
			EndDialog( IDCANCEL );
			return TRUE;
		}
	}

	// get priority
	if( !appConfig.m_SQL.GetDescriptionPriority( &Priority, m_nID ))
	{
		EndDialog( IDCANCEL );
		return TRUE;
	}

	// get variables
	m_szID.Format( _T("%ld"), m_nID );
	m_szDescription = Priority.szDescription;
	m_szPrintNotice = Priority.szPrintNotice;
	m_szIconFile = Priority.szIconFile;
	m_dServiceFee = Priority.dServiceFee;
	m_nInHours = Priority.nInHours;
	m_bUserListedRequest = Priority.nUserListed & ID_LIST_ON_REQUEST;
	m_bUserListedResubmit = Priority.nUserListed & ID_LIST_ON_RESUBMIT;
	m_szTextColor = Priority.szTextColor;
	m_szTextColorOld = Priority.szTextColorOld;
	m_szBackGroundColor = Priority.szBackGroundColor;
	m_szBackGroundColorOld = Priority.szBackGroundColorOld;

	// set index
	for( INT nIndex = 0; nIndex < m_naID.GetCount(); nIndex++ )
		if( m_nID == m_naID.GetAt( nIndex ))
		{
			m_nIndexID = nIndex;
			m_ctrlRecord.SetPos( nIndex );
			break;
		}
	// no index found
	if( m_nIndexID < 0 )
		m_ctrlRecord.EnableWindow( FALSE );

	// update data
	UpdateData( FALSE );

	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

void CDlgEditPriority::OnOK()
{
	// update record
	UpdateRecord();
	// close dialog
	CDialog::OnOK();
}

void CDlgEditPriority::OnCancel()
{
	// TODO: Add your specialized code here and/or call the base class

	CDialog::OnCancel();
}

void CDlgEditPriority::OnBnClickedButtonApply()
{
	// update record
	UpdateRecord();
}

void CDlgEditPriority::OnBnClickedButtonDelete()
{
	// update data
	UpdateData();

	// get library id
	m_nID = _tstol( m_szID );

	// check for 0
	if( m_nID == 0 )
	{
		CString szMessage;
		AfxFormatString1( szMessage, IDS_ERROR_DELETE_ITEM_0, _T("Priority") );
		AfxMessageBox( szMessage );
		return;
	}

	// check if sure
	if( AfxMessageBox( IDS_PRIORITY_DELETE, MB_YESNO ) == IDNO )
		return;

	// remove row
	appConfig.m_SQL.RemoveRow( CMySQLEx::priority, m_nID );

	// adjust requests
	appConfig.m_SQL.UpdateFieldValue( CMySQLEx::requests, _T("priority_id"), m_nID, 0 );

	// remove from array
	m_naID.RemoveAt( m_nIndexID );
	// set spin control
	m_ctrlRecord.SetRange( 0, m_naID.GetCount() - 1 );
	// reset index
	m_nIndexID = 0;
	m_nID = m_naID.GetAt( m_nIndexID );

	// get priority
	PRIORITY_LOOKUP Priority;
	if( !appConfig.m_SQL.GetDescriptionPriority( &Priority, m_nID ))
	{
		EndDialog( IDCANCEL );
		return;
	}

	// get variables
	m_szID.Format( _T("%ld"), m_nID );
	m_szDescription = Priority.szDescription;
	m_szPrintNotice = Priority.szPrintNotice;
	m_szIconFile = Priority.szIconFile;
	m_dServiceFee = Priority.dServiceFee;
	m_nInHours = Priority.nInHours;
	m_bUserListedRequest = Priority.nUserListed & ID_LIST_ON_REQUEST;
	m_bUserListedResubmit = Priority.nUserListed & ID_LIST_ON_RESUBMIT;
	m_szTextColor = Priority.szTextColor;
	m_szTextColorOld = Priority.szTextColorOld;
	m_szBackGroundColor = Priority.szBackGroundColor;
	m_szBackGroundColorOld = Priority.szBackGroundColorOld;

	//update data
	UpdateData( FALSE );
}

void CDlgEditPriority::OnBnClickedButtonBrowse()
{
	// open ico or bmp file	
	TCHAR szFilter[] = _T("Icon Files (*.ico)|*.ico|Bitmap Files (*.bmp)|*.bmp|All Files (*.*)|*.*||");
	CFileDialog fileDlg( TRUE, NULL, NULL, OFN_HIDEREADONLY, szFilter );
	// show dialog
	if( fileDlg.DoModal() == IDOK )
	{
		m_szIconFile = fileDlg.GetPathName();
		// update data
		UpdateData( FALSE );
	}
}

void CDlgEditPriority::OnDeltaposSpinRecord(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMUPDOWN pNMUpDown = reinterpret_cast<LPNMUPDOWN>(pNMHDR);
	// increment index
	m_nIndexID += pNMUpDown->iDelta;
	if( m_nIndexID > m_naID.GetUpperBound() )
	{
		Beep( 750, 300 );
		m_nIndexID = m_naID.GetUpperBound();
		//m_nIndexID = 0;
		return;
	}
	if( m_nIndexID < 0 )
	{
		Beep( 750, 300 );
		m_nIndexID = 0;
		//m_nIndexID = (INT)m_naID.GetUpperBound();
		return;
	}

	// get priority
	PRIORITY_LOOKUP Priority;
	if( !appConfig.m_SQL.GetDescriptionPriority( &Priority, m_naID.GetAt( m_nIndexID )))
	{
		EndDialog( IDCANCEL );
		return;
	}

	// get id
	m_nID = Priority.nID;

	// get variables
	m_szID.Format( _T("%ld"), m_nID );
	m_szDescription = Priority.szDescription;
	m_szPrintNotice = Priority.szPrintNotice;
	m_szIconFile = Priority.szIconFile;
	m_dServiceFee = Priority.dServiceFee;
	m_nInHours = Priority.nInHours;
	m_bUserListedRequest = Priority.nUserListed & ID_LIST_ON_REQUEST;
	m_bUserListedResubmit = Priority.nUserListed & ID_LIST_ON_RESUBMIT;
	m_szTextColor = Priority.szTextColor;
	m_szTextColorOld = Priority.szTextColorOld;
	m_szBackGroundColor = Priority.szBackGroundColor;
	m_szBackGroundColorOld = Priority.szBackGroundColorOld;

	//update data
	UpdateData( FALSE );

	*pResult = 0;
}

BOOL CDlgEditPriority::UpdateRecord()
{
	// update data
	UpdateData();

	// check data
	if( m_szID.IsEmpty() ||	m_szDescription.IsEmpty() )
	{
		AfxMessageBox( _T("Data missing.\n\nPlease complete all data fields.") );
		return FALSE;
	}

	// get priority id
	m_nID = _tstol( m_szID );

	// check for overwrite
	if( m_bAddPriority )
	{
		if( appConfig.m_SQL.GetRowCount( CMySQLEx::priority, m_nID ) > 0 )
		{
			if( AfxMessageBox( IDS_PRIORITY_CHANGE, MB_YESNO ) == IDNO )
			{
				// return
				return FALSE;
			}
		}
	}

	CStringArray szaPriority;
	PRIORITY_LOOKUP Priority;
	Priority.nUserListed = 0;

	// set variables
	Priority.nID = m_nID;
	Priority.szDescription = m_szDescription;
	Priority.szPrintNotice = m_szPrintNotice;
	Priority.szIconFile = m_szIconFile;
	Priority.dServiceFee = m_dServiceFee;
	Priority.nInHours = m_nInHours;
	if( m_bUserListedRequest )
		Priority.nUserListed |= ID_LIST_ON_REQUEST;
	else
		Priority.nUserListed &= ~ID_LIST_ON_REQUEST;
	if( m_bUserListedResubmit )
		Priority.nUserListed |= ID_LIST_ON_RESUBMIT;
	else
		Priority.nUserListed &= ~ID_LIST_ON_RESUBMIT;
	Priority.szTextColor = m_szTextColor;
	Priority.szTextColorOld = m_szTextColorOld;
	Priority.szBackGroundColor = m_szBackGroundColor;
	Priority.szBackGroundColorOld = m_szBackGroundColorOld;

	// save it
	appConfig.m_SQL.SetDescriptionPriority( &Priority, m_nID, m_bAddPriority );

	// fetch collection
	appConfig.m_SQL.GetDescriptions( CMySQLEx::priority, &szaPriority, CMySQLEx::list, &m_naID );

	// set spin control
	m_ctrlRecord.SetRange( 0, m_naID.GetCount() - 1 );
	// set index
	for( INT nIndex = 0; nIndex < m_naID.GetCount(); nIndex++ )
		if( m_nID == m_naID.GetAt( nIndex ))
		{
			m_nIndexID = nIndex;
			m_ctrlRecord.SetPos( nIndex );
			break;
		}
	// no index found
	if( m_nIndexID < 0 )
		m_ctrlRecord.EnableWindow( FALSE );

	// update data
	UpdateData( FALSE );
	// return true
	return TRUE;
}
